package tt.dz.egobus.servicemanager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import tt.dz.egobus.api.managerservice.manager.DistributeDriverTask;
import tt.dz.egobus.api.managerservice.manager.RequestTask;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.cache.service.ExtendCacheService;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.point.BusPoint;
import tt.dz.egobus.servicemanager.service.BusinessCacheService;

/**
 * @author Anson
 * @date 17-3-27
 * @name redis实现派单业务缓存处理
 * @since 1.0.3
 */
@Service("redisBusinessCacheServiceImpl")
public class RedisBusinessCacheServiceImpl implements BusinessCacheService {

    @Autowired
    @Qualifier("extendsRedisServiceImpl")
    private ExtendCacheService cacheService;


    @Override
    public void updateBusiness(BusinessCache businessCache) {
        String key = this.businessKey(businessCache);
        if (key == null) {
            return;
        }
        this.cacheService.set(key, businessCache.toJson());
        this.cacheService.expire(key, BusinessCache.BUSINESS_CACHE_EXPIRE);
    }

    @Override
    public void updateStatus(String businessId, BusinessStatus status) {
        BusinessCache cache = this.get(businessId);
        if (cache == null) {
            return;
        }
        cache = BusinessCache.newBuilder(cache)
                .setStatus(status).builder();
        this.updateBusiness(cache);
    }

    @Override
    public BusinessCache get(String businessId) {
        String businessKey = this.businessKey(businessId);
        String json = this.cacheService.get(businessKey);
        return json == null ? null : BusinessCache.toBean(json);
    }

    @Override
    public void deleteBusiness(String businessId) {
        String businessKey = this.businessKey(businessId);
        this.cacheService.del(businessKey);
    }

    @Override
    public void updateBusiness(RequestTask requestTask, DistributeDriverTask distributeDriverTask, BusPoint[] busPoints) {
        if (distributeDriverTask == null || requestTask == null) {
            return;
        }
        BusinessCache.BusinessCacheBuilder businessCacheBuilder = BusinessCache.newBuilder();
        businessCacheBuilder.setBusinessId(distributeDriverTask.getBusinessId())
                .setDrivers(busPoints)
                .setUserId(distributeDriverTask.getUserId())
                .setRequestTask(requestTask);
        BusinessCache businessCache = businessCacheBuilder.builder();

        this.updateBusiness(businessCache);
    }

    private String businessKey(BusinessCache businessCache) {
        if (businessCache == null) {
            return null;
        }
        return this.businessKey(businessCache.getBusinessId());
    }

    private String businessKey(String businessId) {
        return BusinessCache.idGenerator(businessId).id();
    }


}
